﻿using System;
using System.Collections.Generic;
using System.Text;
using Fsb.Utility;

namespace VideoCache.Spider.FileFetchers
{
    class SohuFetcher : IFetcher
    {
        const string JSON_URL = "http://hot.vrs.sohu.com/vrs_flash.action?vid={0}";

        public FetcherResult[] GetFiles(string url)
        {
            string html = HttpUtility.Get(url, Encoding.GetEncoding("gb2312"));

            int vid;

            if (!int.TryParse(StringUtility.SubString(html, "var vid=\"", "\""), out vid)) return null;

            SohuData data = this.GetData(vid);

            List<FetcherResult> list = new List<FetcherResult>();
            if (data != null)
            {
                if (data.Data.NorVid != 0)
                {
                    SohuData norData = GetData(data.Data.NorVid);
                    if (norData != null)
                    {
                        list.Add(GetResult(norData));
                    }
                }

                if (data.Data.HighVid != 0)
                {
                    SohuData highData = GetData(data.Data.HighVid);
                    if (highData != null)
                    {
                        list.Add(GetResult(highData));
                    }
                }

                if (data.Data.SuperVid != 0)
                {
                    SohuData superData = GetData(data.Data.SuperVid);
                    if (superData != null)
                    {
                        list.Add(GetResult(superData));
                    }
                }
            }

            if (list.Count == 0)
            {
                throw new FetcherException("无法获取视频下载地址，视频不存在或已删除。");
            }

            return list.ToArray();
        }

        private SohuData GetData(int vid)
        {
            string json = HttpUtility.Get(string.Format(JSON_URL, vid), Encoding.UTF8);

            SohuData data = JsonUtility.Object<SohuData>(json);

            return data;
        }

        private FetcherResult GetResult(SohuData data)
        {
            FetcherResult r = new FetcherResult();
            if (data.Id == data.Data.NorVid) r.Resolution = Resolution.Level_1;
            else if (data.Id == data.Data.HighVid) r.Resolution = Resolution.Level_2;
            else if (data.Id == data.Data.SuperVid) r.Resolution = Resolution.Level_3;
            // else if (data.Id == data.Data.OriVid) r.Resolution = Resolution.Level_6; // 原画

            List<string> files = new List<string>();

            for (int i = 0; i < data.Data.TotalBlocks; i++)
            {
                string file = StringUtility.SubString(data.Data.ClipsURL[i], "http://data.vod.itc.cn", "");

                r.Files.Add(string.Format("http://{0}/?file={1}&new={2}", data.Allot, file, data.Data.Su[i]));
            }

            return r;
        }

        private class SohuData
        {
            public int Id { get; set; }
            public string Allot { get; set; }
            public string ReserveIp { get; set; }
            public Data Data { get; set; }
        }

        private class Data
        {
            public string TvName { get; set; }
            public string SubName { get; set; }
            public string[] ClipsURL { get; set; }
            public long[] ClipsBytes { get; set; }
            public decimal[] ClipsDuration { get; set; }
            public int RelativeId { get; set; }
            public int NorVid { get; set; }
            public int HighVid { get; set; }
            public int SuperVid { get; set; }
            public int OriVid { get; set; }
            public int TotalBlocks { get; set; }
            public string[] Ck { get; set; }
            public string[] Hc { get; set; }
            public string[] Su { get; set; }
        }
    }
}
